function t(t, a, i) {
    return a in t ? Object.defineProperty(t, a, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[a] = i, t;
}

var a = getApp(), i = require("../../E99BC573A71FB5DF8FFDAD745E5CA4D1.js"), e = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js"), r = require("../../0BACB9C3A71FB5DF6DCAD1C4BE4CA4D1.js"), o = require("../../68E89321A71FB5DF0E8EFB26C66CA4D1.js").upload;

Page({
    data: {
        winWidth: a.globalData.windowWidth,
        productImg: {},
        md5UploadStatus: {},
        color: 1,
        clothing: 1,
        position: 1,
        skuKeyData: [],
        defaultData: [],
        curAttr: {},
        curAttrText: {},
        attrList: [],
        attrLen: {},
        attrKeys: {},
        skuResult: {},
        cantSelectedItems: {},
        clothingSrc: {},
        sizeData: {},
        initTeamSizeData: JSON.stringify({}),
        teamSizeData: {},
        quantity: 1,
        curSizeData: {},
        inventory: [],
        cartNum: 0,
        isLogin: a.globalData.isLogin,
        isLogining: !1,
        colorObj: {},
        clothImgObj: {},
        productId: null,
        productPrice: 0,
        decalsData: {},
        fromPage: ""
    },
    onLoad: function(t) {
        var i = this, e = t.clothing, r = t.color, o = t.productImg, n = t.decals, s = t.position, d = t.from;
        try {
            o = JSON.parse(decodeURIComponent(o));
        } catch (t) {}
        o = o || {};
        try {
            n = JSON.parse(decodeURIComponent(n));
        } catch (t) {}
        n = n || {};
        var u = {}, c = {};
        for (var l in o) {
            var h = o[l];
            h.print && (u[l] = JSON.parse(JSON.stringify(h)), c[h.printMd5] = !1, c[h.coverMd5] = !1);
        }
        e || (e = 1), e *= 1, r || (r = 1), r *= 1, d || (d = ""), s *= 1, this.setData({
            clothing: e,
            color: r,
            position: s,
            productImg: u,
            md5UploadStatus: c,
            decalsData: n,
            fromPage: d
        }, function() {
            var t = a.globalData.defaultClothingData;
            t ? i.setDefaultData(t) : i.getDefaultData();
        });
        var g = a.globalData.productSkuData;
        g && this.setData({
            inventory: g
        });
    },
    onShow: function() {
        var t = this;
        null !== r._cb && (this.setData({
            isLogining: !1
        }), r._cb = null);
        var i = a.globalData.isLogin;
        this.setData({
            isLogin: i
        }, function() {
            i && t.getCartNum();
        });
    },
    setDefaultData: function(t) {
        var a = this, i = {}, e = {}, r = !0, o = !1, n = void 0;
        try {
            for (var s, d = t[Symbol.iterator](); !(r = (s = d.next()).done); r = !0) {
                var u = s.value, c = {};
                e[u.id] = u.goods_spec[0].positive_picture.thumb;
                for (var l = 0; l < u.goods_spec.length; l++) {
                    var h = u.goods_spec[l];
                    if (h.idx = l, c[h.color_id] = h, !i[h.color_id]) {
                        var g = {
                            id: h.color_id,
                            value: h.color_value
                        };
                        i[h.color_id] = g;
                    }
                }
            }
        } catch (t) {
            o = !0, n = t;
        } finally {
            try {
                !r && d.return && d.return();
            } finally {
                if (o) throw n;
            }
        }
        this.setData({
            defaultData: t,
            colorObj: i,
            clothImgObj: e
        }, function() {
            a.getSkuData();
        });
    },
    getDefaultData: function() {
        var t = this;
        e.get("/v2/goods").then(function(i) {
            if (1001 === i.data.code) {
                var e = i.data.response.data;
                a.globalData.defaultClothingData = e, t.setDefaultData(e);
            }
        });
    },
    getSkuData: function() {
        var t = this;
        this.data.inventory.length > 0 ? (this.getSizeData(), this.getSkuResult()) : (wx.showLoading({
            title: "加载中...",
            mask: !0
        }), e.get("/v2/productAttribute").then(function(a) {
            wx.hideLoading(), 1001 === a.data.code && t.setData({
                inventory: a.data.response.data
            }, function() {
                t.getSizeData(), t.getSkuResult();
            });
        }).catch(function() {
            wx.hideLoading();
        }));
    },
    getSkuResult: function() {
        var t = this, a = this.data.inventory;
        if (a && a.length) {
            var e = i.getSkuResult(a, this.data.position), r = e.skuResult, o = e.skuKeyData, n = {}, s = [], d = {}, u = {}, c = !0, l = !1, h = void 0;
            try {
                for (var g, f = o[Symbol.iterator](); !(c = (g = f.next()).done); c = !0) {
                    var p = g.value;
                    s.push(p.type), u[p.type] = p.attr.length, "clothing" === p.type ? n.clothing = this.data.clothing : "color" === p.type ? n.color = this.data.color : 1 === p.attr.length ? n[p.type] = 1 * p.attr[0].id : n[p.type] = null;
                    var v = !0, m = !1, y = void 0;
                    try {
                        for (var D, S = p.attr[Symbol.iterator](); !(v = (D = S.next()).done); v = !0) {
                            var _ = D.value;
                            d[p.type + "/" + _.id] = _;
                        }
                    } catch (t) {
                        m = !0, y = t;
                    } finally {
                        try {
                            !v && S.return && S.return();
                        } finally {
                            if (m) throw y;
                        }
                    }
                }
            } catch (t) {
                l = !0, h = t;
            } finally {
                try {
                    !c && f.return && f.return();
                } finally {
                    if (l) throw h;
                }
            }
            r = i.initSKU(r, s, "/"), this.setData({
                skuResult: r,
                skuKeyData: o,
                curAttr: n,
                attrList: s,
                attrLen: u,
                attrKeys: d
            }, function() {
                t.getCantSelectedItems(), t.fillShirtAttr();
            });
        }
    },
    getSizeData: function() {
        var a = this.data.inventory;
        if (a && a.length) {
            var i = {}, e = {}, r = !0, o = !1, n = void 0;
            try {
                for (var s, d = a[Symbol.iterator](); !(r = (s = d.next()).done); r = !0) {
                    var u = s.value;
                    if (!i[u.goods_list.id] || i[u.goods_list.id] && !i[u.goods_list.id][u.size_list.id]) {
                        var c = JSON.parse(JSON.stringify(u.size_list));
                        c.quantity = 0, i[u.goods_list.id] = Object.assign(i[u.goods_list.id] || {}, t({}, c.id, c));
                    }
                    var l = u.goods_id + "_" + u.color_id + "_" + u.style_id, h = JSON.parse(JSON.stringify(u.size_list));
                    h.quantity = 0, e[l] ? e[l][u.size_id] = h : e[l] = t({}, h.id, h);
                }
            } catch (t) {
                o = !0, n = t;
            } finally {
                try {
                    !r && d.return && d.return();
                } finally {
                    if (o) throw n;
                }
            }
            this.setData({
                sizeData: i,
                teamSizeData: e,
                initTeamSizeData: JSON.stringify(e)
            });
        }
    },
    getCantSelectedItems: function() {
        var t = this, a = this.data, e = a.curAttr, r = a.attrList, o = a.attrKeys, n = a.skuResult;
        this.setData(i.getCantSelectedItems(e, r, o, n), function() {
            return t.changeProductPrice();
        });
    },
    fillShirtAttr: function() {
        var t = this, a = this.data, i = a.skuKeyData, e = a.curAttr, r = this.data.fromPage, o = "", n = {}, s = {}, d = !0, u = !1, c = void 0;
        try {
            for (var l, h = i[Symbol.iterator](); !(d = (l = h.next()).done); d = !0) {
                var g = l.value, f = !0, p = !1, v = void 0;
                try {
                    for (var m, y = g.attr[Symbol.iterator](); !(f = (m = y.next()).done); f = !0) {
                        var D = m.value;
                        if (1 * D.id == 1 * e[g.type]) {
                            "team" !== r && "size" === g.type && (n = D), ("team" === r && "size" !== g.type || "team" !== r) && (o += D.name + "，", 
                            s[g.type] = D.name);
                            break;
                        }
                    }
                } catch (t) {
                    p = !0, v = t;
                } finally {
                    try {
                        !f && y.return && y.return();
                    } finally {
                        if (p) throw v;
                    }
                }
            }
        } catch (t) {
            u = !0, c = t;
        } finally {
            try {
                !d && h.return && h.return();
            } finally {
                if (u) throw c;
            }
        }
        this.setData({
            productAttr: o.length > 0 ? o.slice(0, -1) : "",
            curSizeData: n,
            curAttrText: s
        }, function() {
            t.getClothingSrc();
        });
    },
    getClothingSrc: function() {
        var t = this.data.defaultData, a = this.data.color, i = this.data.clothing, e = {}, r = !0, o = !1, n = void 0;
        try {
            for (var s, d = t[Symbol.iterator](); !(r = (s = d.next()).done); r = !0) {
                var u = s.value;
                if (1 * u.id == 1 * i) if (a) {
                    var c = !0, l = !1, h = void 0;
                    try {
                        for (var g, f = u.goods_spec[Symbol.iterator](); !(c = (g = f.next()).done); c = !0) {
                            var p = g.value;
                            if (1 * p.color_id == 1 * a) {
                                e = {
                                    1: p.positive_picture,
                                    2: p.opposite_picture
                                };
                                break;
                            }
                        }
                    } catch (t) {
                        l = !0, h = t;
                    } finally {
                        try {
                            !c && f.return && f.return();
                        } finally {
                            if (l) throw h;
                        }
                    }
                } else e = {
                    1: u.goods_spec[0].positive_picture,
                    2: u.goods_spec[0].opposite_picture
                };
                if (e[1]) break;
            }
        } catch (t) {
            o = !0, n = t;
        } finally {
            try {
                !r && d.return && d.return();
            } finally {
                if (o) throw n;
            }
        }
        this.setData({
            clothingSrc: e
        });
    },
    changeShirtAttr: function(t) {
        var a = this, i = t.currentTarget.dataset, e = i.id, r = i.type;
        e *= 1;
        var o = this.data.curAttr, n = this.data.attrLen, s = this.data.fromPage, d = {};
        if ("clothing" === r && o[r] !== e) {
            "team" === s ? d.teamSizeData = JSON.parse(this.data.initTeamSizeData) : (o.size = null, 
            d.quantity = 1, d.curSizeData = {});
            for (var u in n) "clothing" !== u && "size" !== u && n[u] > 1 && (o[u] = null);
        }
        o[r] !== e && (o[r] = e), d.curAttr = o, "clothing" === r ? d.clothing = e : "color" === r && (d.color = e), 
        this.setData(d, function() {
            a.getCantSelectedItems(), a.fillShirtAttr();
        });
    },
    getIsCanSubmit: function() {
        var t = this.data.curAttr, a = this.data.fromPage, i = !0;
        for (var e in t) if (("team" !== a || "size" !== e) && !t[e]) {
            i = !1;
            break;
        }
        return i;
    },
    changeProductPrice: function() {
        var t = this.data, a = t.attrList, i = t.curAttr, e = t.skuResult, r = [], o = !0, n = !1, s = void 0;
        try {
            for (var d, u = a[Symbol.iterator](); !(o = (d = u.next()).done); o = !0) {
                var c = d.value;
                for (var l in i) if (i[l] && l === c) {
                    r.push(l + "/" + i[l]);
                    break;
                }
            }
        } catch (t) {
            n = !0, s = t;
        } finally {
            try {
                !o && u.return && u.return();
            } finally {
                if (n) throw s;
            }
        }
        var h = [];
        e[r = r.join(",")] && (h = e[r].prices);
        var g = 0, f = 0, p = h.length;
        p > 0 && (f = h[0], g = h[p - 1]), this.setData({
            productPrice: f === g ? f / 100 : f / 100 + "~" + g / 100
        });
    },
    lookShirtSize: function() {
        wx.navigateTo({
            url: "/pages/shirt/shirtSize"
        });
    },
    bindInputNumblur: function(t) {
        var a = 1 * t.detail.value;
        a || (a = 1), this.setData({
            quantity: a
        });
    },
    bindInputTeamNumblur: function(t) {
        var a = 1 * t.detail.value, i = t.target.dataset.id, e = this.data.teamSizeData, r = this.data.curAttr;
        a || (a = 0), e[r.clothing + "_" + r.color + "_" + r.type][i].quantity = a, this.setData({
            teamSizeData: e
        });
    },
    bindInputTeamNumClick: function() {
        wx.showToast({
            title: "请先选择上面衣服的属性",
            icon: "none"
        });
    },
    handleNum: function(t) {
        var a = t.currentTarget.dataset.type, i = this.data.quantity;
        if ("add" === a) i += 1; else {
            if (1 === i) return;
            i -= 1;
        }
        this.setData({
            quantity: i
        });
    },
    handleTeamNum: function(t) {
        var a = t.currentTarget.dataset.type, i = t.currentTarget.dataset.id, e = this.data.teamSizeData, r = this.data.curAttr, o = r.clothing + "_" + r.color + "_" + r.type, n = e[o][i].quantity;
        if ("add" === a) n += 1; else {
            if (0 === n) return;
            n -= 1;
        }
        e[o][i].quantity = n, this.setData({
            teamSizeData: e
        });
    },
    navToCart: function() {
        wx.navigateTo({
            url: "/pages/cart/index"
        });
    },
    login: function(t) {
        var i = this;
        a.globalData.isLogin || this.data.isLogining ? "function" == typeof t && t() : this.setData({
            isLogining: !0
        }, function() {
            r.login(function(a) {
                i.setData({
                    isLogining: !1
                }), 1001 === a && "function" == typeof t && t();
            });
        });
    },
    getCartNum: function() {
        var t = this;
        e.get("/v2/shopping/shoppingNum").then(function(a) {
            1001 === a.data.code && t.setData({
                cartNum: a.data.response.data.number
            });
        });
    },
    getTeamBuyingNum: function(t) {
        if (!t) return 0;
        var a = 0;
        for (var i in t) a += t[i].quantity;
        return a;
    },
    addToCart: function() {
        var t = this;
        this.login(function() {
            return t.comfirmToCreateCartData("cart");
        });
    },
    buyNow: function() {
        var t = this;
        this.login(function() {
            return t.comfirmToCreateCartData("buy");
        });
    },
    comfirmToCreateCartData: function(a) {
        var i = this;
        if (this.getIsCanSubmit()) {
            var e = this.data.curAttr, r = this.data.curAttrText, o = this.data.quantity || 1, n = this.data.teamSizeData[e.clothing + "_" + e.color + "_" + e.type], s = this.data.fromPage, d = this.data.curSizeData;
            d.quantity = o;
            var u = t({}, d.id, d), c = this.data.productId;
            if ("team" !== s) this.handleCreateCartData(c, a, e, r, u); else {
                var l = this.getTeamBuyingNum(n);
                l ? l < 5 ? wx.showModal({
                    title: "提示",
                    confirmText: "确定",
                    cancelText: "继续添加",
                    content: "购买数量没达到团购最低数量5件，是否以正常价格购买？",
                    success: function(t) {
                        t.confirm && i.handleCreateCartData(c, a, e, r, n);
                    }
                }) : this.handleCreateCartData(c, a, e, r, n) : wx.showToast({
                    title: "请先选择衣服规格",
                    icon: "none"
                });
            }
        } else wx.showToast({
            title: "请先选择衣服规格",
            icon: "none"
        });
    },
    handleCreateCartData: function(t, a, i, e, r) {
        var o = this;
        t ? (wx.showLoading({
            mask: !0
        }), this.createCartData(a, i, e, r)) : this.uploadAllImg(function() {
            o.createCartData(a, i, e, r);
        });
    },
    createCartData: function(t, i, r, o) {
        var n = this, s = {
            product_id: this.data.productId,
            goods_id: i.clothing,
            goods_spec_id: this.getGoodsSpecId(i, o)
        }, d = this.data.decalsData.id;
        d && (s.media_drew_id = d);
        var u = {};
        for (var c in o) {
            var l = o[c];
            l.quantity > 0 && (u[l.name] = l.quantity);
        }
        s.product_size = JSON.stringify(u);
        for (var h in r) "size" !== h && (s["product_" + h] = r[h]);
        var g = "/v2/shopping/buyNow";
        "cart" === t && (g = "/v2/shopping/add"), e.post(g, s).then(function(i) {
            wx.hideLoading(), 1001 === i.data.code && ("buy" === t ? wx.navigateTo({
                url: "/pages/order/order?cartId=" + i.data.response.data.id
            }) : (wx.showToast({
                icon: "none",
                title: "加入购物车成功"
            }), n.getCartNum(), a.globalData.isNeedToUpdateCartData = !0));
        }).catch(function() {
            wx.hideLoading();
        });
    },
    getGoodsSpecId: function(t, a) {
        var i = this.data.inventory, e = [], r = !0, o = !1, n = void 0;
        try {
            for (var s, d = i[Symbol.iterator](); !(r = (s = d.next()).done); r = !0) {
                var u = s.value;
                for (var c in a) if (1 * u.goods_id == 1 * t.clothing && 1 * u.color_id == 1 * t.color && 1 * u.style_id == 1 * t.type && 1 * u.size_id === a[c].id && a[c].quantity > 0) {
                    e.push(u.id);
                    break;
                }
            }
        } catch (t) {
            o = !0, n = t;
        } finally {
            try {
                !r && d.return && d.return();
            } finally {
                if (o) throw n;
            }
        }
        return e.join(",");
    },
    uploadAllImg: function(t) {
        var a = this, i = this.data.productImg, r = !1, o = [];
        for (var n in i) {
            var s = i[n];
            s.printResult && s.coverResult || (o.push(s.printMd5), o.push(s.coverMd5), r = !0);
        }
        r ? (wx.showLoading({
            title: "上传图案中...",
            mask: !0
        }), e.post("/v2/media/isUpload", {
            md5: o.join(",")
        }).then(function(e) {
            if (1001 === e.data.code) {
                var r = e.data.response.data, o = a.data.md5UploadStatus;
                for (var n in i) {
                    var s = i[n];
                    i[n].printData = r[s.printMd5], r[s.printMd5].id && (i[n].printResult = r[s.printMd5], 
                    o[s.printMd5] = !0), i[n].coverData = r[s.coverMd5], r[s.coverMd5].id && (i[n].coverResult = r[s.coverMd5], 
                    o[s.coverMd5] = !0);
                }
                a.setData({
                    productImg: i,
                    md5UploadStatus: o
                }, function() {
                    a.uploadAllPrint(t);
                });
            } else wx.hideLoading();
        }).catch(function() {
            wx.hideLoading();
        })) : this.data.productId && "function" == typeof t ? (t(), wx.hideLoading()) : this.submitDesign(t);
    },
    uploadAllPrint: function(t) {
        var a = this.data.productImg, i = !1;
        for (var e in a) if (!a[e].printUrl) {
            i = !0, this.uploadDesignImg(e, "print", t);
            break;
        }
        i || this.uploadAllCover(t);
    },
    uploadAllCover: function(t) {
        var a = this.data.productImg, i = !1, e = [];
        for (var r in a) {
            var o = a[r];
            if (!o.coverUrl) {
                i = !0, this.uploadDesignImg(r, "cover", t);
                break;
            }
            o.printResult || e.push({
                md5: o.printMd5,
                url: o.printUrl,
                file_size: o.printSize
            }), o.coverResult || e.push({
                md5: o.coverMd5,
                url: o.coverUrl,
                file_size: o.coverSize
            });
        }
        i || (e.length ? this.getProudctMediaData(t) : this.submitDesign(t));
    },
    uploadDesignImg: function(t, a, i) {
        var e = this, r = this.data.productImg, n = r[t][a + "Md5"], s = this.data.md5UploadStatus, d = r[t][a + "Data"], u = new Date(), c = d.dir + ("print" === a ? "printing" : "original") + "/" + u.getFullYear() + (u.getMonth() < 9 ? "0" : "") + (u.getMonth() + 1) + (u.getDate() < 10 ? "0" : "") + u.getDate() + "/" + n + ".png";
        if (s[n]) r[t][a + "Url"] = c, this.setData({
            productImg: r
        }, function() {
            e.uploadAllPrint(i);
        }); else {
            var l = r[t][a], h = {
                name: l,
                key: c,
                policy: d.policy,
                OSSAccessKeyId: d.accessid,
                success_action_status: "200",
                signature: d.signature
            };
            o(d.host, l, h, {
                fullPath: !0,
                noToken: !0
            }).then(function(o) {
                200 === o.statusCode ? (r[t][a + "Url"] = c, e.setData({
                    productImg: r
                }, function() {
                    e.uploadAllPrint(i);
                })) : wx.hideLoading();
            }).catch(function() {
                wx.hideLoading();
            });
        }
    },
    getProudctMediaData: function(t) {
        var a = this, i = this.data.productImg, r = [];
        for (var o in i) {
            var n = i[o];
            n.printResult || r.push({
                md5: n.printMd5,
                url: n.printUrl,
                file_size: n.printSize
            }), n.coverResult || r.push({
                md5: n.coverMd5,
                url: n.coverUrl,
                file_size: n.coverSize
            });
        }
        e.post("/v2/media/add", {
            img: JSON.stringify(r)
        }).then(function(e) {
            if (1001 === e.data.code) {
                var r = e.data.response.data;
                for (var o in i) {
                    var n = i[o];
                    i[o].printResult || (i[o].printResult = r[n.printMd5]), i[o].coverResult || (i[o].coverResult = r[n.coverMd5]);
                }
                a.setData({
                    productImg: i
                }, function() {
                    a.submitDesign(t);
                });
            } else wx.hideLoading();
        }).catch(function() {
            wx.hideLoading();
        });
    },
    submitDesign: function(t) {
        var a = this, i = this.data.productImg, r = new Date(), o = r.getFullYear() + (r.getMonth() < 9 ? "0" : "") + (r.getMonth() + 1) + (r.getDate() < 10 ? "0" : "") + r.getDate(), n = {
            product_title: o,
            product_attr: 0,
            product_desc: o
        };
        i[1] && (n.product_printimg_media_id = i[1].printResult.id, n.product_coverimg_media_id = i[1].coverResult.id, 
        i[1].product_title = o, i[1].product_desc = o), i[2] && (n.product_back_printimg_media_id = i[2].printResult.id, 
        n.product_back_coverimg_media_id = i[2].coverResult.id, i[2].product_title = o, 
        i[2].product_desc = o), e.post("/v2/product/diy/create", n).then(function(e) {
            1001 === e.data.code && a.setData({
                productId: e.data.response.data.product_id,
                productImg: i
            }, function() {
                "function" == typeof t && t();
            });
        }).catch(function(t) {
            wx.hideLoading();
        });
    }
});